package com.fedoom.frontend.views;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.fedoom.frontend.controllers.EngineController;
import com.fedoom.frontend.models.LevelSettingModel;

import java.io.File;

/**
 * Single player view class
 * for changing single player game settings
 * Extends the ModalView class I created
 * @author tony
 *
 */
@SuppressWarnings("serial")
public class SingleView extends ModalView implements ActionListener{
	
	//Data members
	JTextArea pwadText;
	JLabel difficultyText;
	JLabel levelText;
	
	//Constants
	final String[] difficultyValues = {"default","1", "2", "3", "4", "5"};
	private String prevPwadText;
	private JComboBox difficultyComboBox;
	private JComboBox levelComboBox;
	private int prevDifficulty=-1;
	private int prevLevel=-1;
	
	public SingleView(JFrame owner)
	{
		/**
		 * @param owner - the jframe that 'contains' this modal dialog
		 */
		super(owner);
		setSize(600,100);
		pwadText = new JTextArea();
		difficultyText = new JLabel("Select difficulty") ;
		levelText = new JLabel("Warp to level");
//		pwadText.setEditable(false);
		pwadText.setLineWrap(true);
		LevelSettingModel levelSettingModel= new LevelSettingModel();
		
		JButton iwadButton = new JButton();
		difficultyComboBox = new JComboBox(difficultyValues);
		levelComboBox = levelSettingModel.getComponentAndList();
		
		BorderLayout b = new BorderLayout();
		iwadButton.setLayout(b);
		
		iwadButton.addActionListener(this);
		
		iwadButton.add(new JLabel("Choose additional files..."), BorderLayout.NORTH);
		iwadButton.add(new JLabel("(PWAD or DeHackEd, etc)"), BorderLayout.SOUTH);
		
		difficultyComboBox.setSelectedIndex(0);
		levelComboBox.setSelectedIndex(0);
		
		this.setCommandFieldNames();
		
		this.addLabelComponentPair(iwadButton, pwadText);
		this.addLabelComponentPair(difficultyText, difficultyComboBox);
		this.addLabelComponentPair(levelText, levelComboBox);
		
		this.setName(EngineController.single);
		this.setTitle("Start Single Player");
		this.pack();
	}
	/**
	 * method for setting names for command fields
	 */
	private void setCommandFieldNames()
	{
		//these names can be used to know what component or field is selected 
		pwadText.setName("2");						//changed from "-file" to "2"
		this.difficultyComboBox.setName("11");		//changed from "-difficulty" to "11"
		this.levelComboBox.setName("0");			//changed from "-map" to "0"
	}
	/**
	 * constructor - avoid using this constructor
	 */
	public SingleView(){
		this(null);
	}
	@Override
	public void actionPerformed(ActionEvent e) 
	{

			fileChooser.setMultiSelectionEnabled(true);
			int returnVal = this.fileChooser.showOpenDialog(this);
			
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				
	            String s="";
	            File[] files = fileChooser.getSelectedFiles();
	            for (int i=0;i<files.length;i++)
	            {
	            	s=s+"\""+files[i]+"\" ";
	            }
	            this.pwadText.setText(s);
//	            System.out.println(s);
			}
	
	}

	/**
	 * setVisible was overridden in order to store the existing values of 
	 * the text areas before they are edited in the dialog
	 */
	public void setVisible(boolean b)
	{
		if (b==true)
		{
			prevPwadText = new String (this.pwadText.getText());
			prevDifficulty = difficultyComboBox.getSelectedIndex();
			prevLevel = this.levelComboBox.getSelectedIndex();
		}
		
		super.setVisible(b);
	}

	/**
	 * dispose was overridden to prevent the UI from retaining values when 
	 * a user hits the X to exit a window.
	 * I tested closing windows via OK & via the X.
	 * Only the X calls dispose, so this code won't execute when OK is clicked
	 */
	public void dispose()
	{
		if (prevPwadText!=null && pwadText!=null)
			if (!prevPwadText.equals(pwadText.getText()))
				pwadText.setText(prevPwadText);
		if(this.difficultyComboBox!=null && prevDifficulty>=0)
			if(this.difficultyComboBox.getSelectedIndex()!= this.prevDifficulty)
				this.difficultyComboBox.setSelectedIndex(this.prevDifficulty);
		if(this.levelComboBox!=null && this.prevLevel>=0)
			if(this.levelComboBox.getSelectedIndex()!= this.prevLevel)
				this.levelComboBox.setSelectedIndex(this.prevLevel);

		super.dispose();
	}
	
}
